package com.im.gateway.tcp.dispatcher;

import com.im.common.ImConstant;
import com.im.common.Request;
import com.im.common.Response;
import com.im.gateway.tcp.JedisManager;
import com.im.protocol.AuthenticateRequestProto;
import com.im.protocol.GroupMessageRequestProto;
import com.im.protocol.MessageRequestProto;
import com.im.protocol.PushMessageResponseProto;
import io.netty.channel.socket.SocketChannel;
import redis.clients.jedis.Jedis;

/**
 * 代表分发系统的一个实例
 * @Created leijun
 * @Date 2021/9/16 6:22 上午
 */
public class DispatcherInstance {
    /**
     * 与分发系统建立的socketChannel
     */
    private SocketChannel socketChannel;
    /**
     * 分发系统的地址信息
     */
    private DispatcherAddress dispatcherAddress;

    public DispatcherInstance(SocketChannel socketChannel, DispatcherAddress dispatcherAddress) {
        this.socketChannel = socketChannel;
        this.dispatcherAddress = dispatcherAddress;
    }

    /**
     * 向分发系统进行认证
     * @param authenticateRequest
     */
    public void authenticate(AuthenticateRequestProto.AuthenticateRequest authenticateRequest) {
        Request request = new Request(ImConstant.APP_SDK_VERSION,
                ImConstant.REQUEST_TYPE_AUTHENTICATE,
                ImConstant.SEQUENCE_DEFAULT,
                authenticateRequest.toByteArray());
        socketChannel.writeAndFlush(request.getBuf());
    }

    /**
     * 发送单聊消息
     * @param messageRequest
     */
    public void sendMessage(MessageRequestProto.MessageRequest messageRequest) {
        Jedis jedis = JedisManager.getJedis();
        String sequencekeySuffix = messageRequest.getSenderId() > messageRequest.getReceiverId() ?
                messageRequest.getReceiverId()+"_"+messageRequest.getSenderId() :
                messageRequest.getSenderId()+"_"+messageRequest.getReceiverId();
        String sequencekey = "sequence::single::" + sequencekeySuffix;
        Long sequence = jedis.hincrBy(sequencekey, String.valueOf(messageRequest.getSenderId()), 1l);
        Request request = new Request(ImConstant.APP_SDK_VERSION,
                ImConstant.REQUEST_TYPE_SEND_MESSAGE,
                sequence, messageRequest.toByteArray());
        socketChannel.writeAndFlush(request.getBuf());
    }

    /**
     * 发送群聊消息
     * @param messageRequest
     */
    public void sendGroupMessage(GroupMessageRequestProto.GroupMessageRequest messageRequest) {
        Jedis jedis = JedisManager.getJedis();
        String sequencekey = "sequence::group::" + messageRequest.getGroupId();
        Long sequence = jedis.hincrBy(sequencekey, String.valueOf(messageRequest.getGroupId()), 1l);
        Request request = new Request(ImConstant.APP_SDK_VERSION,
                ImConstant.REQUEST_TYPE_GROUP_MESSAGE,
                sequence, messageRequest.toByteArray());
        socketChannel.writeAndFlush(request.getBuf());
    }

    /**
     * 推送消息响应
     * @param pushMessageResponse
     */
    public void pushMessageResponse(PushMessageResponseProto.PushMessageResponse pushMessageResponse) {
        Response response = new Response(ImConstant.APP_SDK_VERSION,
                ImConstant.REQUEST_TYPE_PUSH_MESSAGE,
                ImConstant.SEQUENCE_DEFAULT,
                pushMessageResponse.toByteArray());
        socketChannel.writeAndFlush(response.getBuf());
    }

    public SocketChannel getSocketChannel() {
        return socketChannel;
    }

    public void setSocketChannel(SocketChannel socketChannel) {
        this.socketChannel = socketChannel;
    }

    public DispatcherAddress getDispatcherAddress() {
        return dispatcherAddress;
    }

    public void setDispatcherAddress(DispatcherAddress dispatcherAddress) {
        this.dispatcherAddress = dispatcherAddress;
    }
}
